import { language } from "./locales"

let errorNum = 0
let allElement = 0
let titleStr = []
let subjectTitle = ''

export function detectingNodes() {
    const currentUrl = window.location.href;
    console.log("当前页面的 URL:", currentUrl);
    if (currentUrl) {
        if (currentUrl.includes('https://www.gate.com/zh/login') || currentUrl.includes('https://www.gate.com/login')) {
            // loginElement()
            loginPageElement()
            subjectTitle = '登录页面'
        }
        console.log(currentUrl, 'currentUrl is not defined5');
        if ((currentUrl.includes('https://www.gate.com/zh/myaccount/profile') || currentUrl.includes('https://www.gate.com/myaccount/profile')) && !currentUrl.includes('setup_totp') && !currentUrl.includes('settings') && !currentUrl.includes('security-setting') && !currentUrl.includes('kyc_home') && !currentUrl.includes('sub-account-manage') && !currentUrl.includes('api-key') && !currentUrl.includes('coupon_center') && !currentUrl.includes('point_manage') && !currentUrl.includes('tickets_list')) {
            errorNum = 0
            allElement = 10
            subjectTitle = '个人页面'
            personElement()
            globalElement()
        }
        if (currentUrl.includes('https://www.gate.com/zh/trade') || currentUrl.includes('https://www.gate.com/trade')) {
            errorNum = 0
            allElement = 13
            subjectTitle = '交易页面'
            tradeElement()
            globalElement()
        }
        if ((currentUrl.includes('https://www.gate.com/zh/futures') || currentUrl.includes('https://www.gate.com/futures')) && !currentUrl.includes('https://www.gate.com/zh/futures-') && !currentUrl.includes('https://www.gate.com/futures-')) {
            errorNum = 0
            allElement = 8
            subjectTitle = '合约页面'
            futuresElement()
            globalElement()
        }
    }

}
// import { ConfigProvider } from 'ant-design-vue';
// // const [api] = notification.useNotification();
// import { createApp, h } from 'vue'
// import Notifi from "@/content-scripts/NotifVue.vue";
// import Notification from '@/content-scripts/notification.vue'

// let lastNotifyTime = 0;
// let lastInputNotTime = 0
import { notification } from 'ant-design-vue';
import { getElement } from '@/content-scripts/getElement.js'

function loginPageElement() {
    const userNameInput = document.querySelector(`input.mantine-Input-input[placeholder="${language.originEmailPlace}"]`)
    const userNameContainer = getElement("login.name")[0]
    const pwdContainer = getElement("login.pwd")[0]
    const phoneContainer = getElement("login.phone")[0]
    console.log(userNameInput, userNameContainer, pwdContainer, phoneContainer);
    console.log(!userNameContainer && !phoneContainer, '!userNameContainer && !phoneContainer');

    if (userNameContainer) {
        if (!userNameInput) {
            titleStr.push('用户名输入框节点发生变化。')
            showNotification({
                message: '登录页面需要修改',
                description: '用户名输入框节点发生变化。',
            });
        }
    }
    if (!userNameContainer && !phoneContainer) {
        titleStr.push('用户名输入框或手机号输入框节点发生变化。')
        showNotification({
            message: '登录页面需要修改',
            description: '用户名输入框或手机号输入框节点发生变化。',
        });
    }
    if (pwdContainer) {
        if (!userNameContainer && !phoneContainer) {
            titleStr.push('账户输入框或手机号输入框节点发生变化。')
            showNotification({
                message: '登录页面需要修改',
                description: '账户输入框或手机号输入框节点发生变化。',
            });
        }
    }
}



function personElement() {
    const personInfoContainer = getElement("person.container")[0]
    if (!personInfoContainer) {
        titleStr.push('挂载个人信息节点发生变化。')
        showNotification({
            message: '个人信息页面',
            description: '挂载个人信息节点发生变化。',
        });
    }
    // if (!newPerson) {
    //     showNotification({
    //         message: '个人信息页面',
    //         description: '新手指引节点发生变化。',
    //     });
    // }
    const verifidOne = getElement("person.verifid")[0]
    const verifidTwo = getElement("person.verifid")[0]
    const verifid = [verifidOne, verifidTwo]
    if (!verifid) {
        titleStr.push('是否认证节点发生变化。')
        showNotification({
            message: '个人信息页面',
            description: '是否认证节点发生变化。',
        });
    }
    const secureLevOne = getElement("person.secureLev")[0]
    const secureLevTwo = getElement("person.secureLev")[0]
    const secureLev = [secureLevOne, secureLevTwo]

    if (!secureLev) {
        titleStr.push('安全等级节点发生变化。')
        showNotification({
            message: '个人信息页面',
            description: '安全等级节点发生变化。',
        });
    }
    const tabs = getElement("person.existing.assets.tabs")[0]
    if (!tabs) {
        titleStr.push('已有资产标签节点发生变化。')
        showNotification({
            message: '个人信息页面',
            description: '已有资产标签节点发生变化。',
        });
    }

    const tabTable = getElement("person.existing.assets.table")[0]
    if (!tabTable) {
        titleStr.push('已有资产表格节点发生变化。')
        showNotification({
            message: '个人信息页面',
            description: '已有资产表格节点发生变化。',
        });
    }
    errorNum = errorNum + 5
}

function globalElement() {
    const topAvatarContainer = getElement("header.avatar")[0]
    if (!topAvatarContainer) {
        titleStr.push('顶部头像节点发生变化。')
        showNotification({
            message: '顶部信息',
            description: '顶部头像节点发生变化。',
        });
    }
    const topNameContainer = getElement("header.name")[0]
    if (!topNameContainer) {
        titleStr.push('个人信息弹框名称节点发生变化。')
        showNotification({
            message: '顶部信息',
            description: '个人信息弹框名称节点发生变化。',
        });
    }
    const notifiContainer = getElement("header.email")[0]
    if (!notifiContainer) {
        titleStr.push('个人信息弹框节点发生变化。')
        showNotification({
            message: '顶部信息',
            description: '个人信息弹框节点发生变化。',
        });
    }
    const assetContainer = getElement("header.asset")
    if (!assetContainer) {
        titleStr.push('个人信息弹框30天交易，总资产节点发生变化。')
        showNotification({
            message: '顶部信息',
            description: '个人信息弹框30天交易，总资产节点发生变化。',
        });
    }
    const changeContainer = getElement("header.language")[0]
    if (!changeContainer) {
        titleStr.push('切换语言节点发生变化。')
        showNotification({
            message: '顶部信息',
            description: '切换语言节点发生变化。',
        });
    }
    errorNum = errorNum + 5
    errorToEmail()
}

import { post } from "@/http/http";

async function errorToEmail() {
    if ((errorNum == allElement) && (titleStr.length != 0)) {
        const dataSource = await post('/user/email/send', {
            "subject": subjectTitle,
            "content": titleStr.join(',')
        })
        console.log(dataSource, 'dataSource');
    }
}

function tradeElement() {
    const inStockTabs = document.querySelector(
        ".spot_order_table > .bottom_box > .order_trade_tabs > .tr-font-medium > .bottomBar_active"
    )
    if (inStockTabs) {
        if (inStockTabs.innerHTML.includes(language.tradeHistory)) {
            const searchArea = document.querySelector('.spot_order_table > .bottom_box > .order_types_container')
            if (!searchArea) {
                titleStr.push('成交记录交易区域节点发生变化。')
                showNotification({
                    message: '现货交易',
                    description: '成交记录交易区域节点发生变化。',
                });
            }
            const directionInput = searchArea.querySelector('.mantine-1b20kon > div > .mantine-dcm9q0 > input')
            if (!directionInput) {
                titleStr.push('成交记录搜索条件节点发生变化。')
                showNotification({
                    message: '现货交易',
                    description: '成交记录搜索条件节点发生变化。',
                });
            }
            const timeGroup = searchArea.querySelectorAll('.trade-order-rangepicker-wrapper > div > div > .mantine-15kxfbj > .mantine-7lvkc8 > input')
            if (!timeGroup) {
                titleStr.push('成交记录时间节点发生变化。')
                showNotification({
                    message: '现货交易',
                    description: '成交记录时间节点发生变化。',
                });
            }
            const container = document.querySelector(".tr-trade__table") ||
                document.querySelector('.bottom_box > .bottom-table-container > .table-x-scroll-bar > .OrderTable') ||
                document.querySelector('.bottom_box > .bottom-table-container > .arbitrage-table-container > .OrderTable') ||
                document.querySelector('.bottom_box > .bottom-table-container > .bottom_group_content > .OrderTable') ||
                document.querySelector('.bottom_box > .table-scroll-window > .tr-table__wrapper > .mantine-t7pwby')
            if (!container) {
                titleStr.push('交易表格节点发生变化。')
                showNotification({
                    message: '现货交易',
                    description: '交易表格节点发生变化。',
                });
            }
        }
    }




    const btnGroup = document.querySelectorAll(
        ".mantine-eqpvjs > .mantine-4benpe"
    );
    if (!btnGroup) {
        titleStr.push('买入卖出节点发生变化。')
        showNotification({
            message: '现货交易',
            description: '买入卖出节点发生变化。',
        });
    }
    const text1 = document.querySelector('#trade-assets-container > .assets-detail ')
    if (!text1) {
        titleStr.push('右边资产节点发生变化。')
        showNotification({
            message: '现货交易',
            description: '右边资产节点发生变化。',
        });
    }
    const tradePanel = document.querySelector(
        ".trade-panel-available > div > span"
    );
    if (!tradePanel) {
        titleStr.push('可用节点发生变化。')
        showNotification({
            message: '现货交易',
            description: '可用节点发生变化。',
        });
    }
    // const priceInput = document.querySelectorAll(
    //     ".mantine-1i52ecs > .mantine-ysl0gg"
    // )[0];
    // if (!priceInput) {
    //     showNotification({
    //         message: '现货交易',
    //         description: '价格节点发生变化。',
    //     });
    // }
    const maxPurchasable = document.querySelectorAll(".mantine-1ieokwl > div")[1];
    if (!maxPurchasable) {
        titleStr.push('最大可买节点发生变化。')
        showNotification({
            message: '现货交易',
            description: '最大可买节点发生变化。',
        });
    }
    errorNum = errorNum + 8
}

function futuresElement() {
    const selectGroup = document.querySelectorAll(
        ".future_order_trade_filter_wrap > div > .mantine-1g7e83g > div > .mantine-dcm9q0 > .mantine-wz61vw"
    );
    const roleSelect = selectGroup[0]
    const directionSelect = selectGroup[1]
    // const timeInterval = document.querySelector('.future_order_trade_filter_wrap > .mantine-15he3b8 > .tr-border-c-text-1');
    const dateTimeInput = document.querySelector('.future_order_trade_filter_wrap > .gui_date_a6245a30 > div > span');

    const tabBtn = document.querySelector(
        ".future_order_trade_tabs > .mantine-1h2fl7w > .mantine-iiksm5 > .mantine-9v9hnb > .mantine-vejqrm > button[data-active='true'] > .mantine-11ahvd6 > li"
    );
    // console.log(tabBtn,'tttttaaaabbbbbbbbbtabsss');

    let tabBtnHtml = null
    if (tabBtn.innerHTML.includes('span')) {
        tabBtnHtml = tabBtn.querySelector('span').innerHTML
        // console.log(tabBtnHtml,'tabBtnHtmltabBtnHtmltabBtnHtmltabBtnHtml');
    } else {
        tabBtnHtml = tabBtn.innerHTML
    }

    if (tabBtnHtml.includes(language.tradeHistory)) {
        if (!roleSelect) {
            titleStr.push('全部角色节点发生变化。')
            showNotification({
                message: '合约',
                description: '全部角色节点发生变化。',
            });
        }
        if (!directionSelect) {
            titleStr.push('全部方向节点发生变化。')
            showNotification({
                message: '合约',
                description: '全部方向节点发生变化。',
            });
        }
        if (!dateTimeInput) {
            titleStr.push('表格搜索时间节点发生变化。')
            showNotification({
                message: '合约',
                description: '表格搜索时间节点发生变化。',
            });
        }
        errorNum = errorNum + 3

    }


}

const lastNotifyMap = {}; // 记录每种内容的上次弹出时间

function showNotification({ message, description, placement = 'topLeft' }) {
    const key = message + '|' + description;
    const now = Date.now();
    if (!lastNotifyMap[key] || now - lastNotifyMap[key] > 5000) {
        lastNotifyMap[key] = now;
        notification.info({
            message,
            description,
            placement,
        });
    }
}




